﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Algorithm.Sort
{
    /// <summary>
    /// Base class for kinds of sorters.
    /// </summary>
    public abstract class SorterBase <T>
    {
        #region Support Methods

        protected static void CheckParameters(T[] elements, IComparer<T> comparer)
        {
            if (elements == null)
            {
                throw new ArgumentNullException(MethodBase.GetCurrentMethod().GetParameters()[0].Name);
            }

            if (comparer == null)
            {
                throw new ArgumentNullException(MethodBase.GetCurrentMethod().GetParameters()[1].Name);
            }
        }

        /// <summary>
        /// Swaps two elements in the given array.
        /// </summary>
        /// <param name="elements">The array contains two elements to be swapped.</param>
        /// <param name="alpha">The index for the first element to be swapped.</param>
        /// <param name="beta">The index for the second element to be swapped.</param>
        protected static void Swap(T[] elements, int alpha, int beta)
        {
            if (alpha != beta)
            {
                T temp = elements[alpha];
                elements[alpha] = elements[beta];
                elements[beta] = temp;
            }
        }

        #endregion Support Methods
    }
}
